package de.hdu.myjobapp.ctrl;


import java.util.List;

import org.apache.log4j.Logger;

import de.hdu.myjobapp.dao.UserDao;
import de.hdu.myjobapp.model.*;
import de.hdu.myjobapp.view.MyJobApplication;

/**
 * The AutenticationController handles all authentication Requests
 * Login/Logout
 * Internal Method authenticate
 * @author MTA
 *
 */
public class AuthenticationController extends AbstractBean {
	
	private static final long serialVersionUID = 1L;

	private User user; //Needs to be adapted because the object has the password in it

	

	/**
	 *  get a Logger for that specific Class
	 */
	private transient Logger log = Logger.getLogger(AuthenticationController.class);
	public AuthenticationController() {
		
	}
	
	/**
	 * Try to log in and set credentials.
	 * 
	 * @param loginname login name value retrieved from the form
	 * @param password login password value retrieved from the form
	 */
	public User login(String loginname, String password) {
		log.info("loginname: " + loginname + " password: " + password);

		// Try to authenticate the user.
		user = Authenticate(loginname, password);
		return user;
		

	}
	/**
	 * Used to inform the application that a user has logged off.
	 */
	public void logout() {

		// Sets the current user as NULL and fires an UserChangeEvent.
		MyJobApplication.getProject().setUser(null);
	}
	/**
	 * Authenticates the user and returns the corresponding User object.
	 * @param Username as String
	 * @param password as String
	 * @return Returns the authenticated User Information as user object, On Authentication failure this object is null
	 */
	private User Authenticate(String loginname, String password) {
	//	test.setUserName("Max Mustermann");
		
		UserDao userDao = (UserDao)MyJobApplication.get().getApplicationContext().getBean("userDao");
		user = userDao.findByName(loginname);
		//List<User> userlist = userDao.findAll();
		//MyJobApplication.getProject().getMainWindow().showNotification(user.getUserPassword() + "formfield" + password );
		/*
		 * First of all be sure that you won't get a NullPointerException
		 * TODO don't work too much with the userobject create a beanItem or other Item whatever
		 */
		if(user == null){
			MyJobApplication.getProject().getMainWindow().showNotification("Falscher Benutzername oder Passwort");
			MyJobApplication.getProject().setUser(null);
			return null;
		}else{
			try{
				if(user.getUserPassword().equals(password)){
					MyJobApplication.getProject().setUser((Object)user); //muss noch geändert werden
					MyJobApplication.getProject().getMainWindow().showNotification("Login Successful");
					return user;
				}else{
					MyJobApplication.getProject().getMainWindow().showNotification("Falscher Benutzername oder Passwort");
					MyJobApplication.getProject().setUser(null); //muss noch geändert werden
					return null;
				}
				
			}catch (NullPointerException e){
				MyJobApplication.getProject().getMainWindow().showNotification("Falscher Benutzername oder Passwort");
				MyJobApplication.getProject().setUser(null); //muss noch geändert werden
				return null; //das muss durch die Views abgefangen werden. In jedem Fall falls kein Login möglich ist, dann muss das klar und deutlich gehandlet werden
			}
		}
		
	}

}
